import urllib
import settings
from decimal import *
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.db.models import permalink
from django.core.urlresolvers import reverse
from django.contrib.localflavor.us.models import *


def get_lat_long(location):
	key = settings.GOOGLE_API_KEY
	output = "csv"
	location = urllib.quote_plus(location)
	request = "http://maps.google.com/maps/geo?q=%s&output=%s&key=%s" % (location, output, key)
	data = urllib.urlopen(request).read()
	dlist = data.split(',')
	if dlist[0] == '200':
		return "%s,%s" % (dlist[2], dlist[3])
	else:
		return ''
			
class BusinessType(models.Model):
	name			= models.CharField(_('name'), max_length=100)
	slug			= models.SlugField(_('slug'), unique=True)
	
	class Meta:
		verbose_name = _('business type')
		verbose_name_plural = _('business types')
		db_table = 'merchant_business_type'
		ordering = ['name']
	
	def __unicode__(self):
		return u'%s' % self.name


class Business(models.Model):
	business_type	= models.ForeignKey(BusinessType)
	name			= models.CharField(_('title'), max_length=60)
	slug			= models.SlugField(_('slug'), unique=True)
	description		= models.TextField(_('description'), blank=True)
	address1		= models.CharField(_('address line 1'), max_length=100)
	address2		= models.CharField(_('address line 2'), max_length=100, blank=True)
	city			= models.CharField(_('city'), max_length=100)
	state			= USStateField(_('state'), default='TX')
	zip_code		= models.CharField(_('postal code'), max_length=10)
	country			= models.CharField(_('country code'), max_length=2)
	phone1			= PhoneNumberField(_('main phone'), blank=True)
	phone2			= PhoneNumberField(_('alt phone'), blank=True)
	mobile			= PhoneNumberField(_('mobile phone'), blank=True)
	fax				= PhoneNumberField(_('fax phone'), blank=True)
	tty				= PhoneNumberField(_('tty phone'), blank=True)
	website			= models.URLField(_('website'), blank=True, verify_exists=False)
	email			= models.EmailField(_('email'), blank=True)
	latitude		= models.DecimalField(_('latitude'), max_digits=8, decimal_places=6, null=True, blank=True)
	longitude		= models.DecimalField(_('longitude'), max_digits=8, decimal_places=6, null=True, blank=True)
	validated		= models.BooleanField(_('validated'), default=False)
	notes			= models.TextField(_('notes'), blank=True)
	created			= models.DateTimeField(_('created'), auto_now_add=True)
	modified		= models.DateTimeField(_('modified'), auto_now=True)
	
	class Meta:
		verbose_name = _('business')
		verbose_name_plural = _('businesses')
		db_table = 'merchant_business'
		ordering = ['name']
	
	def __unicode__(self):
		return u'%s' % self.name
	
	def save(self, force_insert=False, force_update=False):
		location = "%s+%s+%s+%s" % (self.address1, self.city, self.state, self.zip_code)
		latitude,longitude = get_lat_long(location).split(',')
		self.latitude = Decimal(latitude)
		self.longitude = Decimal(longitude)
		if not self.latitude:
			location = "%s+%s+%s" % (self.city, self.state, self.zip_code)
			latitude,longitude = get_lat_long(location).split(',')
			self.latitude = Decimal(latitude)
			self.longitude = Decimal(longitude)
		super(Business, self).save(force_insert, force_update) # Call the "real" save() method.
	
	def get_absolute_url(self):
		return reverse('merchant_business_detail', args=[self.slug])